﻿using MvvmFx.Common.ViewModels.Commands;

namespace MvvmFx.Common.ViewModels.Behaviors.ProcessManagement
{
    /// <summary>
    /// A class that represents one crumb in a breadcrumb control.
    /// A Breadcrumb has _text and can be clickable. 
    /// When clicked, it executures a NavigationHandler delegate.
    /// </summary>
    public class Breadcrumb : ViewModelBase
    {
        /// <summary>
        /// The delegate type to execute
        /// </summary>
        public delegate void NavigateHandler();

        private readonly ProcessService _processService;

        private ActionCommand _navigateCommand;
        private readonly IProcessStep _step;
        private bool _isClickable;

        /// <summary>
        /// Create a non-clickable breadcrumb
        /// </summary>
        /// <param name="processService"></param>
        /// <param name="step"></param>
        public Breadcrumb(ProcessService processService, IProcessStep step)
        {
            _processService = processService;
            _step = step;

            _processService.NewPropertyObserver()
                .AddHandler("CurrentStep", ps => OnCanExecute());

            _isClickable = false;

            _navigateCommand = new ActionCommand(
                       param => _processService.Show(_step.Key),
                       param => IsClickable);
        
            _processService.NewPropertyObserver()
                .AddHandler("IsClickable", ps => _navigateCommand.RaiseCanExecuteChanged());
        }

        /// <summary>
        /// Property Key (string)
        /// </summary>
        public string Key
        {
            get
            {
                return _step.Key;
            }
        }

        /// <summary>
        /// Gets the value of the <see cref=”Text”/> property.
        /// </summary>
        public string Text
        {
            get
            {
                return _step.Key;
            }
        }
        /// <summary>
        /// The command to execute when the breadcrumb is clicked on
        /// </summary>
        public ActionCommand Navigate
        {
            get
            {   
                return _navigateCommand;
            }
        }

        /// <summary>
        /// Gets or sets the value of the <see cref=”IsClickable”/> property.
        /// </summary>
        public bool IsClickable
        {
            get { return _isClickable; }
            set
            {
                if (_isClickable != value)
                {
                    _isClickable = value;
                    OnPropertyChanged("IsClickable");

                    //CommandManager.InvalidateRequerySuggested();
                }
            }
        }

        protected virtual void OnCanExecute()
        {
            if (_processService.HasCurrentStep)
            {
                int currentIndex = _processService.Steps.IndexOf(_processService.CurrentStep);

                for (int i = 0; i < _processService.Steps.Count; i++)
                {
                    var step = _processService.Steps[i];
                    if (step.Key == _step.Key)
                    {
                        if (i <= currentIndex)
                        {
                            IsClickable = true;
                            return;
                        }
                    }
                }
            }

            IsClickable = false;
        }
    }
}